#include "DefenseManager.h"

#include "UnitArbitrator.h"
#include "BorderManager.h"
#include "..\Util.h"

namespace BroodWar
{
	namespace Addon
	{
		DefenseManager::DefenseManager(BWSAL::DefenseManager* manager)
		{
			instance = manager;
		}

		DefenseManager::~DefenseManager()
		{
			BWSAL::DefenseManager::destroy();
		}

		DefenseManager::!DefenseManager()
		{
			BWSAL::DefenseManager::destroy();
		}

		void DefenseManager::Create(IUnitArbitrator^ arbirtator, IBorderManager^ borderManager)
		{
			UnitArbitrator^ uArbitrator = DirectCast<UnitArbitrator^>(arbirtator);
			BorderManager^ manager = DirectCast<BorderManager^>(borderManager);
			BWSAL::DefenseManager::destroy();
			s_instance = gcnew DefenseManager(BWSAL::DefenseManager::create(uArbitrator->instance, manager->instance));
		}

		DefenseManager^ DefenseManager::Instance::get()
		{
			return s_instance;
		}

		void DefenseManager::OnOffer(IEnumerable<Unit^>^ units)
		{
			instance->onOffer(ToSet<Unit^, BWAPI::Unit*>(units, &Api::ConvertUnit));
		}
		
		void DefenseManager::OnRevoke(Unit^ unit, double bid)
		{
			instance->onRevoke(ConvertUnit(unit), bid);
		}
		
		void DefenseManager::OnFrame()
		{
			instance->onFrame();
		}
		
		String^ DefenseManager::Name::get()
		{
			return gcnew String(instance->getName().c_str());
		}
	}
}
